package com.cyandpos.pad.morefunction;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.morefunction.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 更多功能adapter
 */

public class PayGridMainSaleAdapter extends BaseAdapter {
    private Context context;
    public List<Integer> imgs = new ArrayList<Integer>();
    public List<Integer> select_imgs = new ArrayList<Integer>();
    private List<String> name = new ArrayList<>();
    private int clickTemp = -1;//标识被选择的item
    private int[] clickedList=new int[30];//这个数组用来存放item的点击状态
    private int[] clicked;
    private boolean mFlag;

    public PayGridMainSaleAdapter(Context mContext, List<Integer> mImgs, List<Integer> mSelectimgs, List<String> name) {
        super();
        this.context = mContext;
        this.imgs = mImgs;
        this.name = name;
        this.select_imgs=mSelectimgs;
        for (int i=0;i<30;i++){
            clickedList[i]=0;      //初始化item点击状态的数组
        }

    }

    @Override
    public int getCount() {
        return imgs.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate
                    (R.layout.mainsale_grid_item, parent, false);
        }

        ImageView iv = BaseViewHolder.get(convertView, R.id.iv_item);
        TextView tv = BaseViewHolder.get(convertView, R.id.tv_item);
        iv.setImageResource(imgs.get(position));
        tv.setText(name.get(position));
        tv.setTextColor(context.getResources().getColor(R.color.pay_no_select_text) );
        if(clickTemp==position){    //根据点击的Item当前状态设置背景
            if(mFlag){
                iv.setImageResource(select_imgs.get(position));
                tv.setTextColor(context.getResources().getColor(R.color.pay_select_text) );
            }else if(!mFlag){
                iv.setImageResource(imgs.get(position));
                tv.setTextColor(context.getResources().getColor(R.color.pay_no_select_text) );
            }
        }
        return convertView;
    }
    public void setSelectPos(int position,boolean flag) {
        clickTemp = position;
        mFlag = flag;
        notifyDataSetChanged();
    }
}

